import React from 'react';
import {
  StatusBar,
  AppRegistry,
  Alert,
  NativeAppEventEmitter,
} from 'react-native';
import { LocaleContextProvider } from './i18n/LocaleContext';
import btoa from './helpers/btoa';
import { SafeContainer } from './rcomponents';
import RootNavigation from './navigation/appNavigation';
import { Provider } from '@ant-design/react-native';
// import {gestureHandlerRootHOC} from 'react-native-gesture-handler';
import Getui from 'react-native-getui';

global.btoa = btoa; // axios 0.19 http auth btoa polyfills
//return root component
const Root = () => {
  return (
    <Provider>
      <LocaleContextProvider>
        <SafeContainer>
          <StatusBar translucent backgroundColor={'rgba(0,0,0,0.2)'} />
          <RootNavigation />
        </SafeContainer>
      </LocaleContextProvider>
    </Provider>
  );
};

//temp workaround for react-native-gesture-handler in react-native 0.61
// take a look https://github.com/react-native-community/releases/issues/140#issuecomment-532819601
//export default Root;
// USING CLASS COMPONENT AS A WORKAROUND FOR HOT RELOADING
export default class App extends React.Component {
  constructor(props) {
    super(props);
    // 初始状态
    this.state = {
      clientId: '',
      version: '',
      status: '',
    };
  }
  render() {
    return <Root />;
  }
  componentDidMount() {
    NativeAppEventEmitter.addListener(
      'receiveRemoteNotification',
      (notification) => {
        //Android的消息类型为payload 透传消息 或者 cmd消息
        switch (notification.type) {
          case 'cid':
            //  console.log("receiveRemoteNotification cid = " + notification.cid)
            console.log('初始化获取到cid', JSON.stringify(notification));
            break;
          case 'payload':
            console.log('payload 消息通知', JSON.stringify(notification));
            break;
          case 'cmd':
            console.log('cmd 消息通知', 'cmd action = ' + notification.cmd);
            break;
          default:
        }
      },
    );

    NativeAppEventEmitter.addListener(
      'clickRemoteNotification',
      (notification) => {
        console.log('点击通知', JSON.stringify(notification));
      },
    );
    AppRegistry.registerComponent('pushDemo', () => App);
    this.updateComponentInfo();
  }
  updateComponentInfo() {
    Getui.clientId((param) => {
      this.setState({ clientId: param });
    });

    Getui.version((param) => {
      this.setState({ version: param });
    });

    Getui.status((param) => {
      let status = '';
      switch (param) {
        case '0':
          status = '正在启动';
          break;
        case '1':
          status = '启动';
          break;
        case '2':
          status = '停止';
          break;
      }
      this.setState({ status: status });
    });
  }
}
